package com.shiguiwu.springmybatis.spring.autowired;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * @description: a
 * @author: stone
 * @date: Created by 2021/3/15 14:39
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.autowired
 */
@Data
@Component
//加上下面注解，当有多个的时候，会首选这个
//@Primary
public class A implements C{

    //@Autowired属性在字段上
    @Autowired
    private B b;

    //通过有参构造，注入
//    @Autowired
    public A(B b) {
        System.out.println( this.getClass()+ "有参构造");
        this.b = b;
    }

    //通过普通方法注入
//    @Autowired
    public void test(B b) {
        System.out.println( this.getClass()+ "普通方法注入。。。。");
        this.b = b;
    }

    //通过setter方法注入，最为常见
//    @Autowired
    public void setB(B b) {
        System.out.println( this.getClass()+ "setter方法注入。。。。");
        this.b = b;
    }

    //Autowired标注在参数上,没有注入的参数用false
//    @Autowired
    public void test1(B b, @Autowired(required = false) String name) {
        System.out.println(this.getClass()+ ":Autowired标注在参数上。。。。");
        System.out.println(name);
        this.b = b;
    }

    public A() {
//        System.out.println( this.getClass()+ "无参构造");
    }
}
